﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using HF.Framework.DataClientDBAgent;
using HF.Framework.Web;
using HF.Framework.SqlDataContract;
using HF.Business.OA;
using System.Text.RegularExpressions;
using HF.User.Data;
using HF.WorkFlow.Const;

public partial class MessageAdd : System.Web.UI.Page
{
    string messageId = "";
    string state = "";
    string UserId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        UserId=User.Identity.Name;
        if (Request["State"] != null)
        {
            state = Request["State"].ToString();
            if (state == WorkFlowConst.STATE_ADD)//新建
                tbxSubmitMan.Text = UserData.GetInstance().GetUserNameById(UserId);
        }
        else
        {
            state = WorkFlowConst.STATE_VIEW;


        }
        
        foreach (Control cl in this.Controls)
        {
            setReadOnly(cl);
        }
        if (Request["messageId"] != null)
        {
            messageId = Request["messageId"].ToString();

            if (!IsPostBack)
            {
                
                OA_MESSAGE oa = new OA_MESSAGE();
                oa.GetOA_MESSAGEInfo(messageId);

                this.txtTitle.Text = oa.Title;
                this.txtContent.Text = oa.Content;
                this.txtSubmitDate.Text = oa.SubmitDate;
                this.txtMessageId.Text = oa.MessageId;
                this.txtType.Text = oa.MsgType;
                this.txtDuration.Text = oa.Duration.ToString();
                this.txtFile.Text = oa.AttachFile;
                this.txtFileName.Text = oa.FileName;
                this.txtNewFileName.Text = oa.NewFileName;
                this.LinkButton1.Text = oa.FileName;
                this.LinkButton1.CommandArgument = oa.NewFileName;
                this.tbxSubmitMan.Text = oa.SubmitMan;
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (state == WorkFlowConst.STATE_ADD)
        {
            int dura = 0;
            string intRule = @"^\d+$";
            bool duration = Regex.IsMatch(this.txtDuration.Text,intRule);
            if(duration)
            {
                dura = Convert.ToInt32(this.txtDuration.Text);
            }
            else
            {
                dura = 0;
            }
            OA_MESSAGE no = new OA_MESSAGE();
            no.SubmitDate = this.txtSubmitDate.Text;
            no.Title = this.txtTitle.Text;
            no.Content = this.txtContent.Text;
            no.MessageId = Guid.NewGuid().ToString();
            no.MsgType = this.txtType.Text;
            no.Duration = dura;
            no.AttachFile = this.txtFile.Text;
            no.FileName = this.txtFileName.Text;
            no.NewFileName = this.txtNewFileName.Text;
            no.SubmitMan = this.tbxSubmitMan.Text;
            no.Insert();


        }
        else
        {
            int dura = 0;
            string intRule = @"^\d+$";
            bool duration = Regex.IsMatch(this.txtDuration.Text,intRule);
            if(duration)
            {
                dura = Convert.ToInt32(this.txtDuration.Text);
            }
            else
            {
                dura = 0;
            }
            OA_MESSAGE no = new OA_MESSAGE();
            no.SubmitDate = this.txtSubmitDate.Text;
            no.Title = this.txtTitle.Text;
            no.Content = this.txtContent.Text;
            no.Duration = dura;
            no.MsgType = this.txtType.Text;
            no.AttachFile = this.txtFile.Text;
            no.MessageId = this.txtMessageId.Text;
            no.FileName = this.txtFileName.Text;
            no.NewFileName = this.txtNewFileName.Text;
            no.SubmitMan = this.tbxSubmitMan.Text;
            no.Update();
        }
        WorkDialog.ShowMessage(this, "保存成功!");
        btnSave.Enabled = false;
    }



    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.close()</script>");
    }
    protected void ButtonConfirm_Click(object sender, EventArgs e)
    {
        //检查上传文件不为空

        if (inputFiles.PostedFile.ContentLength > 0)
        {
            //设定上传文件的保存路径
            string strSaveDir = "../../Files/";
            string strName = this.inputFiles.PostedFile.FileName;
            //取得文件名(抱括路径)里最后一个"."的索引
            int intExt = strName.LastIndexOf(".");
            //取得文件扩展名
            string strExt = strName.Substring(intExt);
            //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复

            //取得文件名(包括路径)里最后一个"\"的索引
            int intPath = strName.LastIndexOf("\\");
            //取得文件名(不包括路径)
            string strNewName = strName.Substring(intPath + 1);
            string filenameid = Guid.NewGuid().ToString();
            //保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
            //注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
            string filenamepath = Server.MapPath(strSaveDir + filenameid + strExt);
            this.inputFiles.PostedFile.SaveAs(filenamepath);
            //得到这个文件的相关属性:文件名,文件类型,文件大小
            labelUpResult.Text = "上传成功！";
            //labelFileName.Text = "文件源：" + strName;
            //labelFileExt.Text = "文件类型：" + inputFile.PostedFile.ContentType + "( " + strExt + " )";
            //labelFileSize.Text = "文件大小：" + (inputFile.PostedFile.ContentLength / 1024).ToString() + " K Byte(s)";
            
            this.txtFile.Text = filenamepath;
            this.txtNewFileName.Text = filenameid+strExt;
            this.txtFileName.Text = strNewName;
            labelUpResult.Text = "上传成功！";
            
        }
    }

    public static void OpenWindowInNewPage(Page curPage, string destUrl)
    {
        string scriptString = string.Format("<script language='JavaScript'>window.open('" + "{0}" + "','_new');<", destUrl);
        scriptString += "/";
        scriptString += "script>";
        if (!curPage.IsStartupScriptRegistered("Startup"))
        {
            curPage.RegisterStartupScript("Startup", scriptString);
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string parms = (sender as LinkButton).CommandArgument.ToString();
        OpenWindowInNewPage(this.Page, @"../../Files/" + parms);

    }

    public void setReadOnly(Control crl)
    {
        if (state == WorkFlowConst.STATE_VIEW)
        {
            string ctrlTypeName = "";
            foreach (Control c in crl.Controls)
            {
                ctrlTypeName = c.GetType().Name;
                switch (ctrlTypeName)
                {
                    case "Button":
                        (c as Button).Enabled = false;
                        break;
                    case "DrapDownList":
                        (c as Button).Enabled = false;
                        break;
                    case "TextBox":
                        (c as TextBox).ReadOnly = true;
                        break;
                }
                if (c.HasControls())
                    setReadOnly(c);
            }
        }
    }


}
